1369C - RationalLee - CodeForces Solution


greedy math sortings two pointers *1400

Please click on ads to support us..

Python Code:

t = int(input())
for i in range(t):
        line = input().split()
    int_line = [int(x) for x in line]
    n, k = int_line
    ns = input().split()
    ns = [int(x) for x in ns]
    ws = input().split()
    ws = [int(x) for x in ws]

        ns.sort()
    ws.sort()

    result = 0
    num_one = ws.count(1)
    if num_one:
        result += 2 * sum(ns[-num_one:])
        ns = ns[:-num_one]
        ws = ws[num_one:]
    ws.reverse()

        start = 0
    end = len(ns) - 1
    for i in range(len(ws)):
        result += ns[end]
        end -= 1
        result += ns[start]
        start += ws[i]-1
    print(result)



C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define N 500005
int main()
{
        int t,n,a[N],i,k,p[N],b,x,y;
        long long int ans;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		b=0;
		for(i=1;i<=n;++i)
		{
		        cin>>a[i];
		}
		sort(a+1,a+n+1);
		for(i=1;i<=k;++i)
		{
		        cin>>p[i];
		        b+=p[i]==1;
		}
		ans=0;
		for(i=n;i>n-b;i--)
		{
		        ans+=a[i]+a[i];
		}
		sort(p+1,p+k+1);
		y=n-b,x=1;
		for(i=k;i>b;i--)
		{
			ans+=a[x]+a[y];
			x+=p[i]-1;
			y--;
		}
		cout<<ans<<"\n";
	}
} 


Comments

Submit
0 Comments
More Questions

673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation